package com.pentajock.docdb.utils;

import com.pentajock.docdb.types.Document;
import java.io.*;
import java.awt.*;
import java.util.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.datatransfer.*;

/** 
 * Puts HTML into Clipboard as Rich Text
 */

public class Klipboard implements Transferable {
	private static final String TEST_HTML = "<b>[bold]</b> <a href=\"http://www.google.com\">[hyperlink]</a> <i>[italics]</i> <font color=red>[color]</font>";
	private String htmlText;
	private ArrayList<DataFlavor> htmlFlavors = new ArrayList<DataFlavor>();
	private boolean success;
	
	public Klipboard(String htmlText) {
		this.success = true;
		this.htmlText = htmlText;
		addFlavors();
	}

	public static void main(String[] args) {
		Toolkit.getDefaultToolkit().getSystemClipboard().setContents(new Klipboard(TEST_HTML), null);
		System.out.println("\n\nCopied text to clipboard.  Try pasting into your program.\n\n");
	}
	
	public static boolean toClipboard(String htmlText) {
		Klipboard k = new Klipboard(htmlText);
		Toolkit.getDefaultToolkit().getSystemClipboard().setContents(k, null);
		return k.isSuccessful();
	}
	
	private static boolean createFileLink(File file) {
		return toClipboard("<A HREF=\"" + file.toURI().toString() + "\">" + file.getName() + "</A>");
	}
	
	public static boolean createLink(Object o) {
		if (o instanceof String)
			return createFileLink(new File((String)o));
		else if (o instanceof Document)
			return createFileLink(((Document)o).toFile());
		else if (o instanceof File)
			return createFileLink((File)o);
		else 
			return false;
	}
	
	public static boolean testClipboard() {
		System.out.println("\n\nTest data put into clipboard.  Try pasting into your program.\n\n");
		return toClipboard(TEST_HTML);
	}

	private void addFlavors() {
		try {
			//htmlFlavors.add(new DataFlavor("text/plain;class=java.lang.String"));
			htmlFlavors.add(new DataFlavor("text/html;class=java.lang.String"));
			htmlFlavors.add(new DataFlavor("text/html;class=java.io.Reader"));
		}
		catch (Exception e) {
			e.printStackTrace();
			success = false;
		}
	}

	public DataFlavor[] getTransferDataFlavors() {
		return htmlFlavors.toArray(new DataFlavor[htmlFlavors.size()]);
	}

	public boolean isDataFlavorSupported(DataFlavor flavor) {
		return htmlFlavors.contains(flavor);
	}
	
	public boolean isSuccessful() {
		return success;
	}
	
	public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException {            
		if (String.class.equals(flavor.getRepresentationClass()))
			return htmlText;
		//else if (String.class.equals(flavor.getHumanPresentableName()))
		//	return "test!";
		else if (Reader.class.equals(flavor.getRepresentationClass()))
			return new StringReader(htmlText);
		//else if (Reader.class.equals(flavor.getHumanPresentableName()))
		//	return "test!";
		success = false;
		throw new UnsupportedFlavorException(flavor);
	}
}
